

<?php

class Product extends Controller {

    function Product() {
        parent::Controller();
        $this->load->model('mod_product');
    }

    //======================this is function for using loading page master====================//

    function manager() {
        if($this->check_session()){
        $get_product['value_product'] = $this->mod_product->select_product();
        $this->load->view('master', $get_product);
        $data['title'] = 'product manager';
        }else{
            redirect('admin/');
            exit();
        }
    }

//==========================this is function  for using productmanager===========================//

    function productmanager() {
        if($this->check_session()){
        $this->load->view('master');
        }else{
            redirect('admin/');
            exit();
        }
    }

    //===========================this function for using edit product===================//

    function edit() {
        if($this->check_session()){
        if ($this->input->post('btn_submit')) {
            $this->mod_product->checkedit();
            $this->session->set_userdata('msedit', 'Edit successfully');
            $this->session->set_userdata('editproduct_ms', '<br/><dl id="system-message"><dd class="message message"><ul><li>Edited Successful!</li></ul></dd></dl>');
            redirect('product/manager');
        } else {
            $data['getresult'] = $this->mod_product->selet_edit();
            $this->load->view('master', $data);
        }
        }else{
            redirect('admin/');
            exit();
        }
    }

    //=====================function is used for add product==================//

    function add() {
        if($this->check_session()){
        $data['title'] = 'this is the add new page';
        $this->load->helper("mgpage");
        $this->form_validation->set_rules('txtpro', 'ProductName', 'required|trim|max_length[50]');
        $this->form_validation->set_rules('txtqauntity', 'qauntity', 'required|trim|numeric|max_length[20]');
        $this->form_validation->set_rules('txtdetail', 'Detail', 'required|trim|max_length[30]');
        $this->form_validation->set_rules('txtpro_price', 'productPrice', 'required|numeric|trim|max_length[20]');

        if ($this->input->post('btn_submit')) {
            if ($this->form_validation->run() == false) {
                $this->load->view('master', $data);
            } else {
                $this->mod_product->addproduct();
                $this->session->set_userdata('addproduct_ms', '<br/><dl id="system-message"><dd class="message message"><ul><li>Add Successful!</li></ul></dd></dl>');
                redirect('product/manager');
            }
        } else {
            $this->load->view('product/add');
        }
        }else{
            redirect('admin/');
            exit();
        }
    }

    //========================this is function for using adding category of product=======================//

    function addcategory() {
        if($this->check_session()){
        $data['title'] = 'this is the add new page';
        $this->load->helper("mgpage");
        $this->form_validation->set_rules('txt_catname', 'Category Name', 'required|trim|max_length[50]');
        //$this->form_validation->set_rules('txt_detail', 'Detail', 'required|trim|max_length[50]');
        if ($this->input->post('btn_submit')) {
            if ($this->form_validation->run() == false) {
                $this->load->view('master', $data);
            } else {
                $this->mod_product->addcategory();
                $this->session->set_userdata('addcategory_ms', '<br/><dl id="system-message"><dd class="message message"><ul><li>Add Categories Successful!</li></ul></dd></dl>');
                redirect('product/productmanager');
            }
        } else {
            $this->load->view('master');
        }
        }else{
            redirect('admin/');
            exit();
        }
    }

    //========================this is using for delete===========================//

    function delete() {
        if($this->check_session()){
        $this->mod_product->delete();
        $this->session->set_userdata('delproduct_ms', '<br/><dl id="system-message"><dd class="message message"><ul><li>Deleted Successful!</li></ul></dd></dl>');
        redirect('product/manager');
        }else{
            redirect('admin/');
            exit();
        }
    }
    
    //========this is function for using checking session=============
    
     public function check_session() {
        if ($this->session->userdata('use_username')) {
            return true;
        } else {
            return false;
        }
    }

}